package com.bdqn.dao.Impl;

import com.bdqn.dao.BaseDao;
import com.bdqn.dao.NewsDao;
import com.bdqn.entity.News;

import java.sql.ResultSet;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <h3>项目名称:homework0223</h3>
 * <p>本API描述:新闻数据处理操作实现类</p>
 * <p>@author : AliyZhang</p>
 * <p>@date : 2021-02-24 15:16</p>
 **/
//继承BaseDao的方法，同时实现NewsDao中的接口
public class NewsDaoImpl extends BaseDao implements NewsDao {
    @Override
    public List<News> queryAll() {
        //生成SQL字符串
        String sql="select * from news_detail";
        //参数数组为空
        //执行查询语句，返回结果集
        ResultSet rs=exectueQuery(sql,null);
        //创建list集合，对查询的结果进行封装
        List<News> list = new ArrayList<>();
        //循环遍历结果集并存入集合
        try {
            while (rs.next()){
                int id=rs.getInt("id");
                int categoryId=rs.getInt("categoryId");
                String title=rs.getString("title");
                String summary=rs.getString("summary");
                String content=rs.getString("content");
                String picPath=rs.getString("picPath");
                String author=rs.getString("author");
                Date createDate=rs.getDate("createDate");
                Date modifyDate=rs.getDate("modifyDate");
                //把结果集中的记录存到集合中
                News news = new News(id, categoryId, title, summary, content, picPath, author, createDate, modifyDate);
                list.add(news);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        //返回list集合
        return list;
    }

    @Override
    public int addNews(News news) {
        //生成SQL字符串
        String sql="insert into news_detail(categoryId,title,summary,content,picPath,author,createDate) values(?,?,?,?,?,?,?)";
        //生成参数数组
        Object[] params={news.getCategoryId(),news.getTitle(),news.getSummary(),news.getContent(),news.getPicPath(),news.getAuthor(),news.getCreateDate()};
        //调用父类BaseDao中的继承的通用的增删改查的方法
        int rows=executeUpdate(sql,params);
        return rows;
    }

    @Override
    public int deleteNews(int id) {
        //生成SQL语句
        String sql="delete from news_detail where id=?";
        //参数数组
        Object[] params=new Object[1];
        params[0]=id;
        //执行删除语句
        int rows=executeUpdate(sql,params);
        return rows;
    }

    @Override
    public int updateNews(News news) {
        //生成SQL语句
        String sql="update news_detail set categoryId=?,title=?,summary=?,content=?,picPath=?,author=?,modifyDate=? where id=?";
        Object[] params=new Object[8];
        params[0]=news.getCategoryId();
        params[1]=news.getTitle();
        params[2]=news.getSummary();
        params[3]=news.getContent();
        params[4]=news.getPicPath();
        params[5]=news.getAuthor();
        params[6]=news.getModifyDate();
        params[7]=news.getId();
        //执行修改语句
        int rows=executeUpdate(sql,params);
        return rows;
    }

    public static void main(String[] args) {
        //1.添加数据
        NewsDaoImpl newsDao = new NewsDaoImpl();
        /*try {
            News news = new News(2, "你好，李焕英", "你好，李焕英",
                    "听说很感人", "",
                    "贾玲", new SimpleDateFormat("yyyy-MM-dd").parse("2021-02-12"));
            int rows = newsDao.addNews(news);
            if(rows>0){
                System.out.println("新闻添加成功！");
            }else{
                System.out.println("新闻添加失败！");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }*/
        //2.添加数据
        /*List<News> list = newsDao.queryAll();
        System.out.println(list+"\t");*/
        //3.修改数据
        /*try {
            News news = news = new News(12, 3, "唐人街探案3", "唐探3", "......", "", "陈思诚", new SimpleDateFormat("yyyy-MM-dd").parse("2021-02-12"));
            int rows = newsDao.updateNews(news);
            if(rows>0){
                System.out.println("数据修改成功！");
            }else {
                System.out.println("数据修改失败！");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }*/
         //4.删除数据
        int rows=newsDao.deleteNews(13);
        if(rows>0){
            System.out.println("数据删除成功！");
        }else{
            System.out.println("数据删除失败！");
        }

    }
}
